// Copyright 2011 Google Inc. All Rights Reserved.

/**
 * @fileoverview This module provides a way to display our site.
 *
 * @author gavaletz@google.com (Eric Gavaletz)
 */


goog.provide('page');


goog.require('goog.dom');
goog.require('goog.events');
goog.require('goog.events.EventType');
goog.require('goog.ui.AnimatedZippy');
goog.require('goog.ui.Button');
goog.require('goog.ui.FlatButtonRenderer');
goog.require('goog.ui.ProgressBar');
goog.require('goog.ui.Zippy');
goog.require('goog.ui.ZippyEvent');


/**
 * Is the user logged in as an administrator?
 *
 * @type {boolean}
 */
page.loggedInAdmin = false;


/**
 * Sets the admin logged in flag.  Usually just used from the server.
 *
 * @param {boolean} b Is the user logged in as an administrator?
 */
page.setLoggedInAdmin = function(b) {
  page.loggedInAdmin = b;
};
//used by the closure compiler to eport this function call
goog.exportProperty(window, 'pageSetLoggedInAdmin', page.setLoggedInAdmin);


/**
 * Is the user logged in?
 *
 * @type {boolean}
 */
page.loggedIn = false;


/**
 * Sets the logged in flag.  Usually just used from the server.
 *
 * @param {boolean} b Is the user logged in?
 */
page.setLoggedIn = function(b) {
  page.loggedIn = b;
};
//used by the closure compiler to eport this function call
goog.exportProperty(window, 'pageSetLoggedIn', page.setLoggedIn);


/**
 * The URL used to log in a user.
 *
 * @type {string}
 */
page.loginUrl = '';


/**
 * Sets the URL used to log in a user.  Usually just used from the server.
 *
 * @param {string} s the URL to be used.
 */
page.setLoginUrl = function(s) {
  page.loginUrl = s;
};
//used by the closure compiler to eport this function call
goog.exportProperty(window, 'pageSetLoginUrl', page.setLoginUrl);


/**
 * The URL used to log out a user.
 *
 * @type {string}
 */
page.logoutUrl = '';


/**
 * Sets the URL used to log out a user.  Usually just used from the server.
 *
 * @param {string} s the URL to be used.
 */
page.setLogoutUrl = function(s) {
  page.logoutUrl = s;
};
//used by the closure compiler to eport this function call
goog.exportProperty(window, 'pageSetLogoutUrl', page.setLogoutUrl);


/**
 * The URL used to display a list of admin results.
 *
 * @type {string}
 */
page.adminResultsUrl = '';


/**
 * Sets the URL used to display a list of admin results.
 *
 * Usually just used from the server.
 *
 * @param {string} s the URL to be used.
 */
page.setAdminResultsUrl = function(s) {
  page.adminResultsUrl = s;
};
//used by the closure compiler to eport this function call
goog.exportProperty(window, 'pageSetAdminResultsUrl', page.setAdminResultsUrl);


/**
 * The URL used to display a list of results.
 *
 * @type {string}
 */
page.resultsUrl = '';


/**
 * Sets the URL used to display a list of results.
 *
 * Usually just used from the server.
 *
 * @param {string} s the URL to be used.
 */
page.setResultsUrl = function(s) {
  page.resultsUrl = s;
};
//used by the closure compiler to eport this function call
goog.exportProperty(window, 'pageSetResultsUrl', page.setResultsUrl);


/**
 * The URL base used to display the details of a result.
 *
 * @type {string}
 */
page.resultDetailUrl = '';


/**
 * Sets the URL base used to display the details of a result.
 *
 * Usually just used from the server.
 *
 * @param {string} s the URL to be used.
 */
page.setResultDetailUrl = function(s) {
  page.resultDetailUrl = s;
};
//used by the closure compiler to eport this function call
goog.exportProperty(window, 'pageSetResultDetailUrl', page.setResultDetailUrl);


/**
 * The URL used to view the developer console.
 *
 * @type {string}
 */
page.developerUrl = '';


/**
 * Sets the URL used to view the developer console.
 *
 * Usually just used from the server.
 *
 * @param {string} s the URL to be used.
 */
page.setDeveloperUrl = function(s) {
  page.developerUrl = s;
};
//used by the closure compiler to eport this function call
goog.exportProperty(window, 'pageSetDeveloperUrl', page.setDeveloperUrl);


/**
 * The user's name to be displayed.
 *
 * @type {string}
 */
page.userName = '';


/**
 * Sets the user's name.  Usually just used from the server.
 *
 * @param {string} s the user's name to be used.
 */
page.setUserName = function(s) {
  page.userName = s;
};
//used by the closure compiler to eport this function call
goog.exportProperty(window, 'pageSetUserName', page.setUserName);


/**
 * Name space for progress components.
 *
 * @type {Object.{*}}
 */
page.progress = {};


/**
 * Name space for result components.
 *
 * @type {Object.{*}}
 */
page.results = {};


/******************************************************************************
 * START EGG PAGE
 *****************************************************************************/


/**
 * Setup the page for an egg test.
 */
page.eggSetup = function() {
  page.content = goog.dom.createDom('div', {'class': 'fixedWidthContent'});
  goog.dom.appendChild(document.body, page.content);
  page.progress.display = goog.dom.createDom('td',
      {'class': 'displayProgress'});
  page.progress.testName = goog.dom.createDom('td', {});
  page.progress.testMessage = goog.dom.createDom('td', {});
  page.progress.progressBar = goog.dom.createDom('td', {});
  var tr0 = goog.dom.createDom('tr', {}, page.progress.display);
  var tr1 = goog.dom.createDom('tr', {'class': 'message'},
      page.progress.testName);
  var tr2 = goog.dom.createDom('tr', {'class': 'message'},
      page.progress.testMessage);
  var tr3 = goog.dom.createDom('tr', {}, page.progress.progressBar);
  var table = goog.dom.createDom('table', {}, tr0, tr1, tr2, tr3);
  goog.dom.appendChild(page.content, table);

  page.pb = new goog.ui.ProgressBar;
  page.pb.setOrientation(goog.ui.ProgressBar.Orientation.HORIZONTAL);
  page.pb.setMaximum(100);
  page.pb.setMinimum(0);
  page.pb.setStep(1);
  page.pb.setValue(0);
  page.pb.render(page.progress.progressBar);
};


/**
 * Display a link to the egg test results.
 *
 * This also displays a very small summary of the results.
 *
 * @param {string} url the url to the result details.
 * @param {number} latency the latency result.
 * @param {number} throughput the throughput result.
 */
page.eggLink = function(url, latency, throughput) {
  goog.dom.removeChildren(page.progress.display);
  page.eggButton = new goog.ui.Button(nsi18n('pageEggButton'),
      goog.ui.FlatButtonRenderer.getInstance());
  page.eggButton.render(page.progress.display);
  page.eggButton.getElement().id = 'eggButton';
  goog.events.listen(page.eggButton, goog.ui.Component.EventType.ACTION,
      function() {window.open(url);});
  page.progressSetTestName(nsi18n('pageEggLate') + latency + ' ms');
  page.progressSetTestMessage(nsi18n('pageEggThr') + throughput + ' Mbps');
  page.progressSetProgressBar(100);
};


/******************************************************************************
 * START FEATHER PAGE
 *****************************************************************************/


/**
 * Set up the page for a feather test.
 */
page.featherSetup = function() {
  page.content = goog.dom.createDom('div', {'class': 'wideContent'});
  goog.dom.appendChild(document.body, page.content);
  page.progress.display = goog.dom.createDom('td',
      {'class': 'displayProgress'});
  page.progress.testName = goog.dom.createDom('td', {});
  page.progress.testMessage = goog.dom.createDom('td', {});
  var tr0 = goog.dom.createDom('tr', {}, page.progress.display);
  var tr1 = goog.dom.createDom('tr', {}, page.progress.testName);
  var tr2 = goog.dom.createDom('tr', {}, page.progress.testMessage);
  var table = goog.dom.createDom('table', {}, tr0, tr1, tr2);
  goog.dom.appendChild(page.content, table);
};


/******************************************************************************
 * START FLOCK PAGE
 *****************************************************************************/


/**
 * Setup and display the page header.
 */
page.headerSetup = function() {
  if (typeof page.header == 'undefined') {
    //header
    if (page.loggedIn) {
      var home = goog.dom.createDom('a', {'href': '/'}, nsi18n('pageHeadHome'));
      var logout = goog.dom.createDom('a', {'href': page.logoutUrl},
          nsi18n('pageHeadLogout') + page.userName);
      var results = goog.dom.createDom('a', {'href': page.resultsUrl},
          nsi18n('pageHeadResults'));
      var developer = goog.dom.createDom('a', {'href': page.developerUrl},
          nsi18n('pageHeadDeveloper'));
      if (page.loggedInAdmin) {
        var adminResults = goog.dom.createDom('a',
            {'href': page.adminResultsUrl}, 'adminResults');
        page.header = goog.dom.createDom('div', {'id': 'header',
          'class': 'header'}, logout, ' | ', results, ' | ', adminResults,
        ' | ', developer, ' | ', home);
      }
      else {
        page.header = goog.dom.createDom('div', {'id': 'header',
          'class': 'header'}, logout, ' | ', results, ' | ', developer,
        ' | ', home);
      }
    }
    else {
      var login = goog.dom.createDom('a', {'href': page.loginUrl},
          nsi18n('pageHeadLogin'));
      var home = goog.dom.createDom('a', {'href': '/'}, 'home');
      page.header = goog.dom.createDom('div', {'id': 'header',
        'class': 'header'}, login, ' | ', home);
    }
    goog.dom.appendChild(document.body, page.header);
  }
};


/**
 * Setup and display the page subHeader.
 *
 * This header actually goes on top of the header, but because of it's smaller
 * font and position it carries less visible weight than the header (hence the
 * name).
 */
page.subHeaderSetup = function() {
  if (typeof page.subHeader == 'undefined') {
    //subHeader
    var blog = goog.dom.createDom('a', {'href': 'http://news.net-score.org',
      'target': '_blank'}, nsi18n('pageSubHeadBlog'));
    var privacy = goog.dom.createDom('a',
        {'href': 'http://info.net-score.org/privacy', 'target': '_blank'},
        nsi18n('pageSubHeadPrivacy'));
    var documentation = goog.dom.createDom('a',
        {'href': 'http://info.net-score.org', 'target': '_blank'},
        nsi18n('pageSubHeadDoc'));
    var feedback = goog.dom.createDom('a',
        {'href': 'http://info.net-score.org/feedback', 'target': '_blank'},
        nsi18n('pageSubHeadFeedback'));
    var contact = goog.dom.createDom('a',
        {'href': 'mailto:info@net-score.org?subject=net-score%20contact'},
        nsi18n('pageSubHeadContact'));
    page.subHeader = goog.dom.createDom('div', {'id': 'subHeader',
      'class': 'subHeader'}, blog, ' | ', privacy, ' | ', documentation,
    ' | ', feedback, ' | ', contact);
    goog.dom.appendChild(document.body, page.subHeader);
  }
};


/**
 * Setup and display the page content section.
 */
page.contentSetup = function() {
  if (typeof page.content == 'undefined') {
    //content
    //EVERYTHING DISPLAYED AFTER THE WELCOME SHOULD GO IN HERE!
    page.content = goog.dom.createDom('div', {'id': 'content',
      'class': 'fixedWidthContent'});
    var wrapper = goog.dom.createDom('div', {'class': 'wrapper'}, page.content);
    goog.dom.appendChild(document.body, wrapper);
  }
};


/**
 * Setup and display the logo.
 */
page.logoSetup = function() {
  if (typeof page.logo == 'undefined') {
    page.logo = goog.dom.createDom('img',
        {'src': '/images/net-score-flat-logo.png',
          'alt': 'net-score', 'id': 'logo'});
    goog.dom.appendChild(document.body, page.logo);
  }
};


/**
 * Setup and display the buttons for starting a test or displaying info.
 */
page.goButtonSetup = function() {
  if (typeof page.goButton == 'undefined') {
    page.goButton = new goog.ui.Button(nsi18n('pageGoButton'),
        goog.ui.FlatButtonRenderer.getInstance());
    page.goButton.render(document.body);
    page.goButton.getElement().id = 'goButton';
    //WTF button
    page.infoButton = new goog.ui.Button(nsi18n('pageInfoButton'),
        goog.ui.FlatButtonRenderer.getInstance());
    page.infoButton.render(document.body);
    page.infoButton.getElement().id = 'infoButton';
  }
  else {
    goog.dom.setProperties(page.logo, {'id': 'logo'});
    page.goButton.setVisible(true);
    page.infoButton.setVisible(true);
  }
};


/**
 * Set up the page for a flock test.
 */
page.welcomeSetup = function() {
  page.logoSetup();
  page.subHeaderSetup();
  page.headerSetup();
  page.contentSetup();
  page.goButtonSetup();
};


/**
 * Get rid of everything we did for the welcome.
 */
page.welcomeHide = function() {
  if (typeof page.header == 'undefined') {
    page.welcomeSetup();
  }
  goog.dom.setProperties(page.logo, {'id': 'logoSmall'});
  page.goButton.setVisible(false);
  page.infoButton.setVisible(false);
};


/**
 * Clear the page content.
 */
page.clearContent = function() {
  goog.dom.removeChildren(page.content);
};

/******************************************************************************
 * START PROGRESS PAGE
 *****************************************************************************/


/**
 * Create and display a progress bar.
 */
page.progressSetup = function() {
  page.clearContent();

  page.progress.display = goog.dom.createDom('td',
      {'class': 'displayProgress'});
  page.progress.testName = goog.dom.createDom('td', {});
  page.progress.testMessage = goog.dom.createDom('td', {});
  page.progress.progressBar = goog.dom.createDom('td', {});
  var tr0 = goog.dom.createDom('tr', {}, page.progress.display);
  var tr1 = goog.dom.createDom('tr', {'class': 'message'},
      page.progress.testName);
  var tr2 = goog.dom.createDom('tr', {'class': 'message'},
      page.progress.testMessage);
  var tr3 = goog.dom.createDom('tr', {}, page.progress.progressBar);
  var table = goog.dom.createDom('table', {}, tr0, tr1, tr2, tr3);
  goog.dom.appendChild(page.content, table);

  page.pb = new goog.ui.ProgressBar;
  page.pb.setOrientation(goog.ui.ProgressBar.Orientation.HORIZONTAL);
  page.pb.setMaximum(100);
  page.pb.setMinimum(0);
  page.pb.setStep(1);
  page.pb.setValue(0);
  page.pb.render(page.progress.progressBar);
};


/**
 * Display an element just above the progress bar.
 *
 * This is used to hold things like the YouTube video, and is sized as such.
 *
 * @param {Element} element the elelemt to be displayed.
 */
page.progressSetDisplay = function(element) {
  goog.dom.removeChildren(page.progress.display);
  goog.dom.appendChild(page.progress.display, element);
};


/**
 * Display a name for the test in progress.
 *
 * @param {string} name the name of the test in progress.
 */
page.progressSetTestName = function(name) {
  goog.dom.setTextContent(page.progress.testName, name);
};


/**
 * Display a status message for the test in progress.
 *
 * @param {string} message the status of the test in progress.
 */
page.progressSetTestMessage = function(message) {
  goog.dom.setTextContent(page.progress.testMessage, message);
};


/**
 * Displays the percentage of the test completed.
 *
 * The value passed to this function should be a number between 0 and 1.
 *
 * @param {number} percent of the test completed.
 */
page.progressSetProgressBar = function(percent) {
  page.pb.setValue(percent);
};


/**
 * Clear the progress.
 */
page.progressClear = function() {
  goog.dom.removeChildren(page.progress.display);
  page.progressSetTestName(nsi18n('pageTestName'));
  page.progressSetTestMessage(nsi18n('pageTestMessage'));
  page.progressSetProgressBar(0);
};

/******************************************************************************
 * START RESULTS PAGE
 *****************************************************************************/


/**
 * Display the results of a test in a zippy.
 *
 * The results are appended to a list of zippy elements in the page.
 * http://closure-library.googlecode.com/svn/docs/class_goog_ui_Zippy.html
 *
 * @param {string} name for the result zippy.
 * @param {Element} content to be displayed in the result zippy.
 *
 * @return {goog.ui.Zippy} the container for the results.
 */
page.resultsAppend = function(name, content) {
  var testName = new goog.ui.Button(name,
      goog.ui.FlatButtonRenderer.getInstance());
  testName.render(page.content);
  var contentWrapper = goog.dom.createDom('div', {}, testName.getElement(),
      content);
  goog.dom.appendChild(page.content, contentWrapper);
  var zipContent = new goog.ui.Zippy(testName.getElement(), content);
  return zipContent;
};


/**
 * Set up the page for displaying a list of results.
 */
page.resultsSetup = function() {
  if (typeof page.header == 'undefined') {
    page.logoSetup();
    page.subHeaderSetup();
    page.headerSetup();
    page.contentSetup();
  }
  goog.dom.setProperties(page.logo, {'id': 'logoSmall'});
  page.clearContent();
  goog.dom.setProperties(page.content, {'class': 'fixedWidthContent'});
};


/**
 * Clear the page content.
 */
page.resultsClear = function() {
  page.clearContent();
};


/******************************************************************************
 * START RESULTS-LIST PAGE
 *****************************************************************************/


/**
 * Display a list of links to results.
 *
 * @param {Element} resultsTable the list of results.
 */
page.resultsListPopulate = function(resultsTable) {
  page.clearContent();
  goog.dom.appendChild(page.content, resultsTable);
};


/**
 * Set up the page for displaying a list of results.
 */
page.resultsListSetup = function() {
  if (typeof page.header == 'undefined') {
    page.logoSetup();
    page.subHeaderSetup();
    page.headerSetup();
    page.contentSetup();
  }
  goog.dom.setProperties(page.logo, {'id': 'logoSmall'});
  page.clearContent();
  goog.dom.setProperties(page.content, {'class': 'wideContent'});
};


/**
 * Clear the page content.
 */
page.resultsListClear = function() {
  page.clearContent();
};


/******************************************************************************
 * START DEVLOPER PAGE
 *****************************************************************************/


/**
 * Set up the page for displaying the developer console.
 */
page.developerSetup = function() {
  if (typeof page.header == 'undefined') {
    page.logoSetup();
    page.subHeaderSetup();
    page.headerSetup();
    page.contentSetup();
  }
  goog.dom.setProperties(page.logo, {'id': 'logoSmall'});
  page.clearContent();
  goog.dom.setProperties(page.content, {'class': 'wideContent'});
};


/**
 * Clear the page content.
 */
page.developerClear = function() {
  page.clearContent();
};

//Needed for something, but I cannot remember what...oops!
goog.events.listen(window, goog.events.EventType.UNLOAD,
    goog.events.removeAll);
